use common::{utils::file_utils, protocol::{XID, protocol_v1::ProtocolV1Reader, MsgType, get_version}};
use lazy_static::lazy_static;

pub mod protocol_handler;
pub mod protocol_hearbeat;
pub mod protocol_v1;

#[derive(PartialEq)]
pub enum ProtocolVersion {
    V1 = 1,
    V6 = 6,
}

lazy_static! {
    pub static ref SUPPORT_PROTOCOL_VERSION: Vec<ProtocolVersion> =
        vec![ProtocolVersion::V1, ProtocolVersion::V6];
}

#[derive(PartialEq)]
pub enum ServiceStatus {
    Normal,
    Busy,
    Free,
}

pub struct ProtocolFieldSize;

impl ProtocolFieldSize {
    pub const DATA_LEN: u32 = 4;
    pub const VERSION: u32 = 2;
    pub const MSG_TYPE: u32 = 2;
    pub const ENV_ID_POS: u32 = 8;
    pub const ORIGIN_REQ_ID: u32 = 8;
    pub const FROM_REQ_ID: u32 = 8;
    pub const CUR_REQ_ID: u32 = 8;
    pub const LAST_REQ_ID: u32 = 8;
    pub const TIME_OUT: u32 = 4;
}

/**
 * 消息 优先级
 */
#[derive(PartialEq)]
pub enum MsgPriority {
    Low,    // 低
    Normal, // 中
    Hig,    // 高
}

// // 组件的语言类型#[derive(Copy, Clone, PartialEq)]
// #[derive(Copy, Clone, PartialEq)]
// pub enum LangType {
//     UnKnow, //未知的类型
//     Cpp,    // C++
//     Python, // Python
//     Java,   // Java
//     CSharp, // C#
//     Go,     // GO
// }

// impl LangType {
//     pub fn string_to_lange_type(lang: &str) -> LangType {
//         match lang {
//             "c++" => LangType::Cpp,
//             "c#" => LangType::CSharp,
//             "java" => LangType::Java,
//             "python" => LangType::Python,
//             "go" => LangType::Go,
//             _ => LangType::UnKnow,
//         }
//     }
// }




pub struct ReqIds {
    pub cur_req_id: XID,
    pub origin_req_id: XID,
    pub opaque_id: XID,
}



/**
 * 获取消息类型
 */
pub fn get_msg_type(message: &Vec<u8>) -> Option<MsgType> {
    let version = get_version(message);
    match version {
        1 => {
            let protocol_reader = ProtocolV1Reader::new(message);
            protocol_reader.msg_type()
        }
        _ => None,
    }
}

pub fn get_xid(message: &Vec<u8>) -> Option<XID> {
    let version = get_version(message);

    match version {
        1 => {
            let protocol_reader = ProtocolV1Reader::new(message);
            Some(protocol_reader.xid())
        }
        _ => None,
    }
}
// /**
//  * 获取请求id
//  */
// pub fn get_req_id(message: &Vec<u8>) -> Option<ReqIds> {
//     let version = get_version(message);
//     match version {
//         1 => {
//             let protocol_reader = ProtocolV1Reader::new(message);
//             Some(ReqIds {
//                 cur_req_id: protocol_reader.xid(),
//                 origin_req_id: XID::default(),
//                 opaque_id: XID::default(),
//             })
//         }
//         _ => None,
//     }
// }

/**
 * 获取存储时间
 */
pub fn get_ttl(message: &Vec<u8>) -> Option<u32> {
    let version = get_version(message);

    match version {
        1 => {
            let protocol_reader = ProtocolV1Reader::new(message);
            Some(protocol_reader.timeout())
        }
        _ => None,
    }
}
